﻿namespace Web.Control.WWW.WebIM
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.ControlsUI;
    using Lx.Web.Common.UI;
    using System.Collections.Generic;
    using BLF.WWW_User.Users;
    using Model.WWW_User.User;
    using System.Collections;
    using System.Text;

    /// <summary>
    /// 2008.05.27 SnaiL Create, WebIM后台CS
    /// </summary>
    public class webim : BaseControl
    {
        public webim() {
        }

        protected override void OnLoad(EventArgs e) {
            ////重复窗口判断
            //if (Session["onlyAwindow"] == null) {
            //    Session["onlyAwindow"] = true;
            //} else {
            //    Response.Write("<script>alert('please do not repeat open the window!');window.close();</script>");
            //    Response.End();
            //    return;
            //}

            //登录判断
            if (HttpContext.Current.User.Identity.Name == "") {
                Page.Response.Redirect(WWWHost + "/User/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri);
                return;
            }

            //如果挂上了startjid，将插入t_talked表
            if (!string.IsNullOrEmpty(Request.QueryString["startjid"])) {
                BLF.WWW_User.Users.User.InsertTalked(HttpContext.Current.User.Identity.Name, Request.QueryString["startjid"]);
            }

            //获得widget id、我的昵称
            string widgetID = BLF.WWW_User.Users.User.GetWidgetID(HttpContext.Current.User.Identity.Name);
            if (string.IsNullOrEmpty(widgetID)) {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('system error!');window.close();</script>");
                return;
            } else {
                string tmp;
                Page.ClientScript.RegisterStartupScript(this.GetType(),"", "<script type='text/javascript'>document.getElementById('hidden_widgetid').value = '" + widgetID + "';MyName=unescape('" + escape(BLF.WWW_User.Users.User.GetUserName(HttpContext.Current.User.Identity.Name, out tmp)) + "');</script>");
            }
            base.OnLoad(e);
        }


        /// <summary>
        /// 2008.06.20 snail create, c#实现javascript的escape函数对MyName进行编码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private string escape(string s) {
            StringBuilder sb = new StringBuilder();
            byte[] ba = System.Text.Encoding.Unicode.GetBytes(s);
            for (int i = 0; i < ba.Length; i += 2) {    /**///// BE SURE 2's 
                sb.Append("%u");
                sb.Append(ba[i + 1].ToString("X2"));
                sb.Append(ba[i].ToString("X2"));
            }
            return sb.ToString();

        }
    }
}
